'''
Created on Jul 9, 2011

@author: kafkef
'''

from PyQt4.QtGui import *
from PyQt4.QtCore import (QObject, SIGNAL, QAbstractTableModel, Qt, QModelIndex,
                          QVariant)


from dexen.system.client import ui
from dexen.system.client.proxy import JobProxy
from dexen.system.common.base import Action
from dexen.system.client.ui.pages.base_job_page import BaseJobTableModel,\
    BaseJobPage



svr_proxy = ui.svr_proxy

#===============================================================================
# Classes
#===============================================================================
class JobLogModel(BaseJobTableModel):
    def __init__(self, parent = None):
        BaseJobTableModel.__init__(self, [], [], [], parent)
            
    def add_logs(self, content_items, content_logs, header):
        position = len(self._content_items)
        rows = len(content_items)
        
        if rows == 0: return

        if self.columnCount() == 0:
            self.beginInsertColumns(QModelIndex(), 0, len(content_items[0]) - 1)
            self.endInsertColumns()
        
        if self._header == []:
            self._header = header.split(",")
            self._content_str = header
        
        self.beginInsertRows(QModelIndex(), position, position + rows - 1)
        self._content_items.extend(content_items)
        self.endInsertRows()
        
        content_str = "\n".join(content_logs)
        self._content_str += "\n" + content_str
    

class JobLogPage(BaseJobPage):       
    def __init__(self):
        BaseJobPage.__init__(self)
        #self._create_widgets()
        #self.log_models = {} #job_id: JobLogModel
        self.cur_act_ids = {} #job_id: action id
        #self._cur_job_id = None
        
    # Override the base class method        
    def update_cur_job_id(self, job_id):
        self._cur_job_id = job_id
        if not self._models.has_key(job_id):
            self._models[job_id] = JobLogModel()
        self._table_view.setModel(self._models[job_id])
        if not self.cur_act_ids.has_key(job_id):
            self.cur_act_ids[job_id] = 0
            self.on_refresh()
    
    # Override the base class method
    def on_refresh(self):
        if self._cur_job_id is None: return
        print "JobLogPage::on_refresh()"
        job_id = self._cur_job_id
        job_proxy = svr_proxy.get_job_cached(job_id)
        assert isinstance(job_proxy, JobProxy)
        actions = job_proxy.get_actions(self.cur_act_ids[job_id])

        print len(actions), "are received"
        
        # Process the new actions
        action_items = []
        action_logs = []
        for action in actions:
            assert isinstance(action, Action)
            log_str = action.get_log()
            log_items = log_str.split(",")
            action_items.append(log_items)
            action_logs.append(log_str)
            
        # Update the model based on the newly processed actions
        if len(action_items) > 0:
            action = actions[0]
            assert isinstance(action, Action)
            header = action.get_header()
            model = self._models[job_id]
            assert isinstance(model, JobLogModel)
            model.add_logs(action_items, action_logs, header)
        
        # Save the last processed action id
        self.cur_act_ids[job_id] = self.cur_act_ids[job_id] + len(actions)
        
        # Update the view based on the new contents
        self._table_view.resizeColumnsToContents()       


        